<template>
  <div>
    <Row>
      <Card>
        <p slot="title">
          <Icon type="navicon-round"></Icon>
          出入园时间设置
        </p>
        <VmBtable type="edit" ref="VmBtable"
                  :columns="dataColumns"
                  :data="dataTable"
                  :searchparam="searchParam"
                  :exportBtn="false"
                  :importBtn="false"
                  :batchDelBtn="false"
                  :downTemplate="false"
                  v-on:searchFn="searchFn"
                  v-on:page-change="pageChangeFn"
                  v-on:addFn="addFn"
                  v-on:editFn="editFn"
                  v-on:deleteFn="deleteFn"
                  placeholder="标题"
                  searchText="标题："
                  cleanText="刷新"
                  exportPath="exportPath"
                  v-on:restFn="restFn">
          <div slot="search" class="db_inline">

          </div>
        </VmBtable>
      </Card>
    </Row>
    <Modal v-model="modal.visible" :title="modal.title" v-on:on-ok="subForm" :mask-closable="false" width="540"
           v-on:on-cancel="cancelSub">
      <Form ref="Form" :model="modal.form" :label-width="100" label-position="right" :rules="modal.rules">
        <FormItem label="名称" prop="name">
          <Input v-model="modal.form.name" placeholder="名称" style="width:63%"/>
        </FormItem>
        <FormItem label="开始时间" prop="startTime">
          <TimePicker v-model="modal.form.startTime" @on-change="($event)" placeholder="上学时间"
                      style="width:63%"></TimePicker>
        </FormItem>
        <FormItem label="结束时间" prop="endTime">
          <TimePicker v-model="modal.form.endTime" @on-change="($event)" placeholder="放学开始时间"
                      style="width:63%"></TimePicker>
        </FormItem>
        <FormItem label="出入设置">
          <RadioGroup v-model="modal.form.direction">
            <Radio :label=0>入园时间段</Radio>
            <Radio :label=1>离园时间段</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="晨检设置">
          <RadioGroup v-model="modal.form.atdType">
            <Radio :label=0>只体检</Radio>
            <Radio :label=1>只考勤</Radio>
            <Radio :label=2>体检+考勤</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="状态" prop="status">
          <RadioGroup v-model="modal.form.status">
            <Radio label='1'>启用</Radio>
            <Radio label='0'>停用</Radio>
          </RadioGroup>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancelSub">取消</Button>
        <Button type="primary" @click="subForm" :loading="modal.loading">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
  import API from "@/api/api";
  import VmBtable from "@/components/vm/vm-btable";
  import Cookie from "js-cookie"

  export default {
    components: {
      VmBtable
    },
    data() {
      return {
        searchParam: {
          current: 1,
          size: 20,
          total: 0,
          username: '',
        },
        exportPath: Cookie.getJSON('cdnUrl'),
        dataColumns: [
          {
            title: '序号',
            type: 'index',
            align: 'center',
            width: 100
          },
          {
            title: '标题',
            key: 'name',
            align: 'center',
          },
          {
            title: '开始时间',
            key: 'startTime',
            align: 'center',
          },
          {
            title: '结束时间',
            key: 'endTime',
            align: 'center',
          },
          {
            title: '方向性质',
            key: 'direction',
            align: 'center',
            render: (h, param) => {
              let status = param.row.direction;
              let des = status === 1 ? '离园时间段' : '入园时间段';
              return h('div', {}, des);
            }
          },
          {
            title: '晨检设置',
            key: 'atdType',
            align: 'center',
            render: (h, param) => {
              let status = param.row.atdType;
              let des = status === 0 ? '只体检' : status === 1 ? '只考勤' : '体检+考勤';
              return h('div', {}, des);
            }
          },
          {
            title: '状态',
            key: 'status',
            align: 'center',
            sortable: true,
            sortType: 'desc',
            render: (h, param) => {
              let status = param.row.status;
              let des = status === '1' ? '启用' : '停用';
              let color = status === '1' ? 'green' : 'orange';
              return h('div', {
                style: {
                  color: color
                }
              }, des);
            }
          },
        ],
        operate: [],
        dataTable: [],
        modal: {
          visible: false,
          title: '',
          isEdit: false,
          loading: false,
          form: {
            leaveType: 0,
            name: '',
            startTime: '',
            endTime: '',
            overTime: '',
            status: '1',
            direction: 0,
            atdType: 0
          },
          rules: {
            'name': [{
              required: true,
              trigger: 'blur',
              message: '必填项'
            }],
            'startTime': [{
              required: true,
              trigger: 'blur',
              message: '必填项'
            }],
            'endTime': [{
              required: true,
              trigger: 'blur',
              message: '必填项'
            }],
          }
        },
      };
    },
    methods: {
      addFn() {
        Object.assign(this.modal, {
          visible: true,
          isEdit: false,
          title: '添加',
        });
        this.modal.form.id = '';
        this.modal.form.direction = 0;
        this.modal.form.atdType = 0;
      },
      editFn(data) {
        Object.assign(this.modal, {
          visible: true,
          isEdit: true,
          title: '编辑',
          form: data
        })
      },
      deleteFn(data) {
        API.deleteStuConfig(data[0].id).then(resp => {
          this.queryFn();
        })
      },
      restFn() {
        this.searchParam.current = 1;
        this.searchParam.size = 20;
        this.queryFn();
      },
      queryFn() {
        API.fetchStuConfig(this.searchParam).then(
          res => {
            let data = res.data;
            this.dataTable = data.records;
            this.searchParam.total = data.total;
          },
          error => {
            this.$Message.info(error.msg);
          }
        );
      },
      complete() {
        Object.assign(this.modal, {
          visible: false,
          title: '',
          loading: false,
        });
        this.$refs.Form.resetFields();
      },
      subForm() {
        this.$refs.Form.validate(valid => {
          if (valid) {
            let data = Object.assign(this.modal.form);
            if (data.id) {
              API.updateStuConfig(data).then(resp => {
                this.complete();
                this.queryFn();
              })
            } else {
              API.createStuConfig(data).then(resp => {
                this.complete();
                this.queryFn();
              })
            }
          }
        })
      },
      cancelSub() {
        this.complete();
      },
      pageChangeFn(current) {
        this.searchParam.current = current;
        this.queryFn();
      },
      searchFn(value) {
        this.searchParam.name = value;
        this.queryFn();
      },
    },
    created() {
    },
    mounted() {
      this.queryFn();
    }
  }
</script>

<style scoped>

</style>
